define(function(require){

    require('../../../c/base/index');

    require('../../../c/nav/index');


    var sms = {
        init : function() {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function(){
                if($(this).parents("a").text() == "短信发送"){
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.bindEvent();
            this.initValidator();
        },

        bindEvent : function() {
            var self = this;
            $("#sms_send_btn").click(function () {
                self.sendSMS();
            });
        },
        initValidator : function() {
            $('#sms_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    sms_mobiles: {
                        message: '电话号码输入错误',
                        validators: {
                            notEmpty: {
                                message: '电话号码不能为空'
                            },
                            phone: {
                                country: 'CN',
                                message: '请输入正确的电话格式'
                            }
                        }
                    },
                    sms_content: {
                        message: '短信内容输入错误',
                        validators: {
                            notEmpty: {
                                message: '短信内容不能为空'
                            }
                        }
                    }
                }
            });
        },
        sendSMS : function() {
            $("#sms_form").data('bootstrapValidator').resetForm();
            $("#sms_form").data('bootstrapValidator').validate();
            var _data = {};
            _data.mobiles = $.trim($("#sms_mobiles").val());
            _data.content = $.trim($("#sms_content").val());
            _data.appid = 1010;
            _data.smsType = 2;
            dataString = JSON.stringify(_data);
            if($("#sms_form").data('bootstrapValidator').isValid()) {
                CRM.request({
                    "type": 'get',
                    "url": CRM.API.smsSend,
                    "dataType": "json",
                    contentType: "application/json",
                    "data": _data,
                    "success": function (req) {
                        if (req.code != "－1") {
                            CRM.showDialog("发送成功！");
                        } else {
                            CRM.showDialog("发送失败");
                        }
                    }
                });
            }
        }

    };

    sms.init();
});

